var categoryPage = avalon.define({
    $id: "categoryPage",
    banners: {},
    categorys: [],
    newRows: 1,
    isBannerEmpty: true,
    toggle: false,
    isHas: function() {
        return false;
    },
    productslistPage: ''
});
mui.ready(function() {
    var slider = mui("#slider");

    mui.init({
        pullRefresh: {
            container: "#category",
            down: {
                height: 50,
                auto: true,
                contentdown: $.i18n.prop('Pull_down_to_refresh'),
                contentover: $.i18n.prop('Release_to_immediate_refresh'),
                contentrefresh: $.i18n.prop('Loading'),
                callback: function() {
                    setTimeout(function() {
                        mui.ajax('/product/index.json', {
                            dataType: 'json',
                            data: { cid: "0" },
                            success: function(data) {
                                // console.log(data)
                                var banners = {};

                                if (data.banners.length > 0) {
                                    categoryPage.isBannerEmpty = false;

                                    var interval = 0;
                                    if (data.banners.length == 1) {
                                        banners['first'] = data.banners[0];
                                        banners['last'] = data.banners[0];
                                    } else {
                                        avalon.each(data.banners, function(i, banner) {
                                            if (0 == i) {
                                                banners['first'] = banner;
                                            } else if (data.banners.length == i + 1) {
                                                banners['last'] = banner;
                                            }
                                        });
                                        interval = 3000;
                                    }
                                    banners['items'] = data.banners;
                                    categoryPage.banners = banners;
                                    slider.slider({
                                        interval: interval
                                    });
                                } else {
                                    categoryPage.isBannerEmpty = true;
                                }
                                categoryPage.categorys = data.categorys;
                                // categoryPage.newRows = data.newRows;
                                 var screenHeight=document.documentElement.clientHeight || document.body.clientHeight;
//                                 alert(screenHeight);
                                 var statusHeight=20;
                                 if(window.screen.height >=812)
                                 {
                                    statusHeight=44 + 34;
                                 }
//                                var height = (window.screen.height - 44 - 160 - 90 - statusHeight) / 5;
                                 var height = (screenHeight - 44 - 160 - 90)/5;
                                $(".mui-table").css("height", height);
                                $(".vm img").css("height", height - 30);
                            }
                        });
                        mui('#category').pullRefresh().endPulldownToRefresh();
                    }, window.DAHUA_CONFIG.LOADING_TIME);
                }
            }
        },
        // 预加载页面productslist.html
        preloadPages: [{
            url: 'productslist.html',
            id: 'productslist.html'
        }],
    });
    var btn = document.getElementById('to');
    btn.addEventListener("tap", function() {
        // console.log("tap event trigger");
        // mui('.mui-off-canvas-wrap').offCanvas('show');
        sessionStorage.setItem('todo', 2);
    });
    mui(".product_box").on('tap', 'a', function(to) {
        var url = this.getAttribute('href');
        var id = this.getAttribute("id");
        var i = this.getAttribute('index');
        mui.openWindow({
            url: url
        });
        sessionStorage.setItem('pb', i);
        sessionStorage.setItem('indexid', id);
    });

    mui("#slider").on('tap', 'a', function() {
        var id = this.getAttribute("data-id");
        if ("" != id) {
            var type = this.getAttribute("data-type");
            var url = null;
            if (1 == type) {
                url = "pdp.html?id=" + id;
            } else {
                url = "list.html?cid=" + id;
            }
            mui.openWindow({
                url: url
            });
        }
    });
    mui(".for-more").on('tap', 'a', function() {
        var url = this.getAttribute('href');
        mui.openWindow({
            url: url
        });
        sessionStorage.setItem('pb', '0AI666')
    });
    // mui(".mui-bar-nav").on('tap', '.mui-icon-white', function() {
    //     var url = this.getAttribute('href')
    //     mui.openWindow({
    //         url: url
    //     })
    // })
});
